GAME.Name = "Pong"
GAME.Author = "Devenger"
GAME.Version = "version 1"
GAME.Selectable = true

local scrSize = tgm.ScreenSize

local court_x = scrSize * 0.05
local court_y = scrSize * 0.25
local court_w = scrSize * 0.9
local court_h = scrSize * 0.7

local player_score_x = scrSize * 0.25
local player_score_y = scrSize * 0.1
local opp_score_x = scrSize * 0.75
local opp_score_y = scrSize * 0.1

local player_bat_x = 0
local opp_bat_x = court_w * 0.97
local bat_w = court_w * 0.03
local bat_h = court_h * 0.2

local ball_size = court_w * 0.03
local ball_speed_start = court_w * 0.012
local ball_speed_increase = 1
local ball_yaxis_deflection = 0.1

surface.CreateFont ("coolvetica", scrSize * 0.12, 500, false, false, "CV60")

function GAME:Initialize ()
	self.Player = {score = 0}
	self.Opp = {score = 0}
	
	self:ResetPositions ()
end

function GAME:ResetPositions ()
	self.Player = {
		y = court_h / 2 - bat_h / 2,
		score = self.Player.score or 0,
		speed = court_w * 0.001
	}
	
	self.Opp = {
		y = court_h / 2 - bat_h / 2,
		score = self.Opp.score or 0,
		speed = court_w * 0.0011
	}
	
	self.Ball = {
		x = court_w / 2,
		y = court_h / 2,
		vx = ball_speed_start,
		vy = ball_speed_start
	}
end

function GAME:Think ()
	--we're going to run this at 100fps - effectively...
	if not self.LastFrame then self.LastFrame = CurTime() - 0.05 end
	local frames = math.floor ((CurTime() - self.LastFrame) * 20)
	for i=1, frames do
		--the player's movement
			local y = math.Clamp (self:MouseY() - court_y - bat_h / 2, 0, court_h - bat_h)
			self.Player.y = self.Player.y * (1 - self.Player.speed) + y * self.Player.speed
			self.LastFrame = self.LastFrame + 0.05
		--the opp. movement
			self.Opp.y = self.Opp.y * (1 - self.Opp.speed) + math.Clamp ((self.Ball.y - bat_h / 2), 0, court_h - bat_h) * self.Opp.speed
		--the ball
			--movement
				self.Ball.x = self.Ball.x + self.Ball.vx
				self.Ball.y = self.Ball.y + self.Ball.vy
				local collision = false
			--ball collisions
				if self.Ball.y + ball_size / 2 > court_h or self.Ball.y - ball_size / 2 < 0 then
					self.Ball.vy = -self.Ball.vy
					collision = true
				end
				if self.Ball.x + ball_size > court_w then
					local relPos = self.Ball.y - (self.Opp.y + bat_h / 2)
					--print (relPos)
					if self.Ball.y + ball_size / 2 > self.Opp.y and self.Ball.y - ball_size / 2 < self.Opp.y + bat_h then
						self.Ball.vx = -self.Ball.vx - ball_speed_increase
						self.Ball.vy = self.Ball.vy + relPos * ball_yaxis_deflection
						collision = true
					else
						self.Player.score = self.Player.score + 1
						self:ResetPositions()
					end
				elseif self.Ball.x - ball_size < 0 then
					local relPos = self.Ball.y - (self.Player.y + bat_h / 2)
					--print (relPos)
					if self.Ball.y + ball_size / 2 > self.Player.y and self.Ball.y - ball_size / 2 < self.Player.y + bat_h then
						self.Ball.vx = -self.Ball.vx + ball_speed_increase
						self.Ball.vy = self.Ball.vy + relPos * ball_yaxis_deflection
						collision = true
					else
						self.Opp.score = self.Opp.score + 1
						self:ResetPositions()
					end
				end
			--movement AGEN
				if collision then
					self.Ball.x = self.Ball.x + self.Ball.vx
					self.Ball.y = self.Ball.y + self.Ball.vy
				end
	end
end

function GAME:Render ()
	--court
	surface.SetDrawColor (100, 100, 100, 255)
 	surface.DrawRect (court_x, court_y, court_w, court_h)
	--scores
	draw.SimpleText(self.Player.score, "CV60", player_score_x, player_score_y, Color(255,255,255,255), TEXT_ALIGN_CENTER)
	draw.SimpleText(self.Opp.score, "CV60", opp_score_x, opp_score_y, Color(255,255,255,255), TEXT_ALIGN_CENTER)
	--render the bats
	surface.SetDrawColor (255, 255, 255, 255)
	surface.DrawRect (court_x + player_bat_x, court_y + self.Player.y, bat_w, bat_h)
	surface.DrawRect (court_x + opp_bat_x, court_y + self.Opp.y, bat_w, bat_h)
	--render the ball
	surface.DrawRect (court_x + self.Ball.x - ball_size * 0.5, court_y + self.Ball.y - ball_size * 0.5, ball_size * 0.5, ball_size * 0.5)
end

function GAME:Pause ()
	print ("pausing")
	self.LastFrame = CurTime() + 9999
end

function GAME:Unpause ()
	print ("unpausing")
	self.LastFrame = CurTime()
end
